package com.lyn.utils;

import java.lang.reflect.Field;

/**
 * A对象非空字段赋值到B对象同名字段
 */
public class ObjectToObjectUtil {

    public static void metastasis(Object objectA, Object objectB) {
        if (objectA == null || objectB == null) {
            return;
        }
        Class<?> objectAClass = objectA.getClass();
        Field[] objectAClassDeclaredFields = objectAClass.getDeclaredFields();
        Class<?> objectBClass = objectB.getClass();
        Field[] objectBClassDeclaredFields = objectBClass.getDeclaredFields();
        for (int i = 0; i < objectAClassDeclaredFields.length; i++) {
            Field fieldA = objectAClassDeclaredFields[i];
            String fieldAName = fieldA.getName();
            fieldA.setAccessible(true);
            Field fieldB = null;
            boolean isExist = false;
            b:
            for (int j = 0; j < objectBClassDeclaredFields.length; j++) {
                fieldB = objectBClassDeclaredFields[j];
                String fieldBName = fieldB.getName();
                if (fieldAName.equals(fieldBName)) {
                    isExist = true;
                    break b;
                }
            }
            if (isExist) {
                try {
                    fieldB.setAccessible(true);
                    Object o = fieldA.get(objectA);
                    if (o==null){
                        continue;
                    }
                    fieldB.set(objectB, o);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void metastasis(Object objectA, Object objectB, boolean isLowerCase) {
        if (objectA == null || objectB == null) {
            return;
        }
        Class<?> objectAClass = objectA.getClass();
        Field[] objectAClassDeclaredFields = objectAClass.getDeclaredFields();
        Class<?> objectBClass = objectB.getClass();
        Field[] objectBClassDeclaredFields = objectBClass.getDeclaredFields();
        for (int i = 0; i < objectAClassDeclaredFields.length; i++) {
            Field fieldA = objectAClassDeclaredFields[i];
            String fieldAName = fieldA.getName();
            if (isLowerCase) {
                fieldAName = fieldAName.toLowerCase();
            }
            fieldA.setAccessible(true);
            Field fieldB = null;
            boolean isExist = false;
            b:
            for (int j = 0; j < objectBClassDeclaredFields.length; j++) {
                fieldB = objectBClassDeclaredFields[j];
                String fieldBName = fieldB.getName();
                fieldBName = fieldBName.toLowerCase();
                if (fieldAName.equals(fieldBName)) {
                    isExist = true;
                    break b;
                }
            }
            if (isExist) {
                try {
                    fieldB.setAccessible(true);
                    Object o = fieldA.get(objectA);
                    if (o==null){
                        continue;
                    }
                    fieldB.set(objectB, o);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
